//
//  TextAnimations.swift
//  animations
//
//  Created by Ekaterina on 13.01.2023.
//

import SwiftUI

struct TextAnimationView: View {
    @State var isLarge = false;
    
    @State var duration = 1.0
    @State var text = "Hello"
    @State var fromSize = 50
    @State var toSize = 100
    
    let minDuration = 0.1
    let maxDuration = 2.0
    
    var body: some View {
        VStack {
            Text(text)
                .lineLimit(1)
                .modifier(AnimatableFontModifier(textSize: isLarge ? Double(fromSize) : Double(toSize)))
            Spacer()
            
            VStack(spacing: 16) {
                VStack(alignment: .leading, spacing: 4) {
                    Text("Text:")
                        .font(.system(size: 16, weight: .semibold))
                    TextField("Text", text: $text).textFieldStyle(.roundedBorder)
                }
                Divider()
                
                HStack {
                    ValuePicker(title: "From size:", minValue: 10, maxValue: 50, value: $fromSize)
                    ValuePicker(title: "To size:", minValue: 51, maxValue: 100, value: $toSize)
                }
                Divider()
                
                ValueSlider(title: "Duration", minValue: minDuration, maxValue: maxDuration, value: $duration)
                
                PlayButton {
                    withAnimation(.easeInOut(duration: duration)) {
                        isLarge.toggle()
                    }
                }
            }
        }
        .navigationTitle("Text Animation")
        .navigationBarTitleDisplayMode(.inline)
        .padding(24)
    }
}

struct AnimatableFontModifier: Animatable, ViewModifier {
    var textSize: Double
    
    var animatableData: Double {
        get { return textSize }
        set {
            textSize = newValue
        }
    }
    
    func body(content: Content) -> some View {
        return content.font(.system(size: textSize))
    }
}

struct TextAnimation_Previews: PreviewProvider {
    static var previews: some View {
        TextAnimationView()
    }
}
